/* -----------------------------------------------------------------------
 * Becky Kluth
 * CS 362
 * Assignment 4: randomtestcard.c to test smithy card (refactored) with random inputs
 *
 * randomtestcard: randomtestcard.c dominion.o rngs.o
 *      gcc -o randomtestcard -g  randomtestcard.c dominion.o rngs.o $(CFLAGS)
 * -----------------------------------------------------------------------
 */

#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"
#include <time.h>
#include <stdlib.h>


#define NOISY_TEST 0												//set NOISY_TEST to 1 to print passing tests as well as the failing tests
#define NUMBER_TESTS 10000											//macro to change the total number of tests run
#define MAX_CARDS 100												//macro to change the max number of cards in player 0's hand + deck + played pile

void testSmithy();

int main(){
	int i;
	printf("Testing smithy card\n");

	for (i = 0; i < NUMBER_TESTS; i++){								//test smithy NUM_TESTS times
		testSmithy();
	}

	return 0;
}	

void testSmithy(){
	int seed = 1000;										
    int numPlayer;				
    int k[10] = {adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, great_hall};
    struct gameState G;
	int startingHandCount, startingDeck, startingPlayed;
	int noCrash;
	int i;

	numPlayer = (rand() % 3) + 2;							//randomly select 2 to 4 players
#if (NOISY_TEST == 1)
     printf("%d players.\n", numPlayer);
#endif

    initializeGame(numPlayer, k, seed, &G);					//initialize new game

	G.handCount[0] = rand() % (MAX_CARDS);					//randomly assign 0 to MAX_CARDS cards in starting hand
	startingHandCount = G.handCount[0];						
	for(i = 0; i < G.handCount[0]; i++){					//fill hand with gold
		G.hand[0][i] = gold;
	}

	G.deckCount[0] = rand() % (MAX_CARDS - startingHandCount);	//assign random number of remaining cards to deck
	startingDeck = G.deckCount[0];							
	for(i = 0; i < G.deckCount[0]; i++){					//fill deck with gold
		G.deck[0][i] = gold;
	}

	G.playedCardCount = rand() % (MAX_CARDS - (startingHandCount + startingDeck));	//assign random number of remaining cards to played
	startingPlayed = G.playedCardCount;							
	for(i = 0; i < G.playedCardCount; i++){					//fill played with gold
		G.playedCards[i] = gold;
	}

#if (NOISY_TEST == 1)
     printf("Test player 0 starts with %d cards in hand, %d in deck, %d in played.\n", startingHandCount, startingDeck, startingPlayed);
#endif

	noCrash = cardEffect(smithy, 0, 0, 0, &G, 1, 0);		//play smithy card, should add 3 cards then discard 1 card for total count change of +2 cards
	if(noCrash != 0){
		printf("Crash!\n");
	}

	if(G.handCount[0] != startingHandCount + 2) {										//failed adding 3 cards to hand, discarding 1
		printf("FAIL: %d players. Start: %d hand, %d deck, %d played. End: %d hand, %d deck, %d played \n", numPlayer, startingHandCount, startingDeck, startingPlayed, 
			G.handCount[0], G.deckCount[0], G.playedCardCount);
	} else if((startingDeck >= 3) && (G.deckCount[0] != startingDeck - 3)){				//failed removing 3 cards from deck when deck >= 3 cards
		printf("FAIL: %d players. Start: %d hand, %d deck, %d played. End: %d hand, %d deck, %d played \n", numPlayer, startingHandCount, startingDeck, startingPlayed, 
			G.handCount[0], G.deckCount[0], G.playedCardCount);
	} else if(G.playedCardCount != startingPlayed + 1){									//failed adding smithy to played pile
		printf("FAIL: %d players. Start: %d hand, %d deck, %d played. End: %d hand, %d deck, %d played \n", numPlayer, startingHandCount, startingDeck, startingPlayed, 
			G.handCount[0], G.deckCount[0], G.playedCardCount);
	} else {																			//passed!
#if (NOISY_TEST == 1)
		 printf("PASS: Test player 0 ends with %d cards in hand, %d in deck, %d in played.\n", G.handCount[0], G.deckCount[0], G.playedCardCount);
#endif
	}

}